package com.pig4cloud.pigx.ruralLibrary.controller;

import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import com.pig4cloud.pigx.ruralLibrary.service.SmsService;
import com.pig4cloud.pigx.ruralLibrary.util.CommonUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * 公共接口
 *
 * @author myg
 * @date 2023-07-26  16:06:24
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/common")
@Tag(description = "common", name = "公共接口")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CommonController {
	@Autowired
	private RedisTemplate redisTemplate;

	@Autowired
	private SmsService smsService;

	@Operation(summary = "注册发送手机验证码", description = "注册发送手机验证码")
	@GetMapping("/code/sms")
	@ResponseBody
	@Inner(false)
	public R sendSmsCode(@Schema(description = "手机号") @RequestParam("phoneNumber") String phoneNumber,
						 HttpServletRequest request) throws IOException {
		String code = CommonUtil.getCode();
		redisTemplate.opsForValue().set(
				"sms-code-".concat(phoneNumber), code,
				SecurityConstants.CODE_TIME, TimeUnit.SECONDS);
		boolean flag = smsService.sendReSms(phoneNumber, code);
		return R.ok(flag);
	}


}
